<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/7/15
 * Time: 11:49
 */

namespace app\controller\api;


use app\controller\Base;
use app\model\User;

class Login extends Base
{
    public function login(){
        $username = input('username');
        $password = input('password');
        if (empty($username))
            return $this->fail('账号不能为空');
        $user = User::where('username',$username)
            ->where('password',$password)
            ->find();
        if (empty($user->id))
            return $this->fail('账号或密码错误');
        $token = md5(uniqid().rand(100,999));
        cache($token,$user);
        return $this->msg($token);
    }

    public function register(){
        $username = input('username');
        $password = input('password');
        $count = User::where('username',$username)->count();
        if (empty($username))
            return $this->fail('账号不能为空');
        if ($count > 0)
            return $this->fail('账号已经存在');
        $user = User::create([
            'username' => $username,
            'password' => $password,
            'level' => 1
        ]);
        return $this->msg($user);
    }
}